#import "YWHotListViewController.h"
#import "LTNetTool.h"
#import "YWHotListModel.h"
#import "YWHotListTableViewCell.h"
#import "YWProgramListModel.h"
#import "YWCirclePhotoViewController.h"

#import "YWSquarePhotoViewController.h"
#import "YWTheMostHotestViewController.h"
#import "YWTheMostSubscribeViewController.h"
#import "YWAnchorHotListViewController.h"

@interface YWHotListViewController ()<UITableViewDelegate, UITableViewDataSource>
/**
 *  榜单数据源
 */
@property (nonatomic, strong) NSMutableArray *programListArray;
/**
 *  图片链接
 */
@property (nonatomic, copy) NSString *picUrl;
/**
 *  HotListTableView
 */
@property (nonatomic, strong) UITableView *hotListTableView;
/**
 *  bigImage
 */
@property (nonatomic, strong) UIImageView *bigImageView;

@end

@implementation YWHotListViewController

- (void)dealloc
{
    self.hotListTableView.delegate = nil;
    self.hotListTableView.dataSource = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getView];
    self.programListArray = [NSMutableArray array];
    [self getData];
}

- (void) getView;
{
    //主体tableView
    self.hotListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - (49 + 64 + 30)) style:UITableViewStyleGrouped];
    _hotListTableView.backgroundColor = [UIColor clearColor];
    _hotListTableView.delegate = self;
    _hotListTableView.dataSource = self;
    _hotListTableView.separatorStyle = NO;

    _hotListTableView.bounces = NO;


    [self.view addSubview:_hotListTableView];
    
    //大图片

    self.bigImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREENHEIGHT, 170)];
    _bigImageView.image = [UIImage imageNamed:@"榜单"];

    _hotListTableView.tableHeaderView = _bigImageView;
    
    
    //注册cell
    [_hotListTableView registerClass:[YWHotListTableViewCell class] forCellReuseIdentifier:@"hotListCell"];
    
    //刷新 加载
    _hotListTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_programListArray removeAllObjects];
        [self getData];
    }];
}

#pragma mark -------------- tableView代理方法 ----------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _programListArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    YWHotListModel *model = [_programListArray objectAtIndex:section];
    return model.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWHotListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hotListCell"];
    cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    YWProgramListModel *model = [[[_programListArray objectAtIndex:indexPath.section] list] objectAtIndex:indexPath.row];
    cell.listModel = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110 * FITHEIGHT;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 40 * FITHEIGHT)];
    headerView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 * FITWIDTH, 12.5 * FITHEIGHT, 15 * FITWIDTH, 15 * FITHEIGHT)];
    arrowImageView.image = [UIImage imageNamed:@"三角"];
    [headerView addSubview:arrowImageView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(35 * FITWIDTH, 12.5 * FITHEIGHT, 100 * FITWIDTH, 15 * FITHEIGHT)];
    label.text = [[_programListArray objectAtIndex:section] title];
    label.font = [UIFont systemFontOfSize:13];
    label.textColor = [UIColor whiteColor];
    [headerView addSubview:label];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40 * FITHEIGHT;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 10 * FITHEIGHT)];
    footView.backgroundColor = [UIColor clearColor];
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10 * FITHEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YWProgramListModel *programListModel = [[[_programListArray objectAtIndex:indexPath.section] list] objectAtIndex:indexPath.row];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            YWTheMostHotestViewController *tmhvc = [[YWTheMostHotestViewController alloc] init];
            tmhvc.titleString = programListModel.title;
            tmhvc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:tmhvc animated:YES];
        }
        else if (indexPath.row == 1)
        {
            YWTheMostSubscribeViewController *tmsvc = [[YWTheMostSubscribeViewController alloc] init];
            tmsvc.titleString = programListModel.title;
            tmsvc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:tmsvc animated:YES];
        }
        else
        {
            if ([programListModel.contentType isEqualToString:@"track"]) {
                YWCirclePhotoViewController *cpvc = [[YWCirclePhotoViewController alloc] init];
                cpvc.key = programListModel.key;
                cpvc.titleString = programListModel.title;
                cpvc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:cpvc animated:YES];
            }
            else{
                YWSquarePhotoViewController *spvc = [[YWSquarePhotoViewController alloc] init];
                spvc.key = programListModel.key;
                spvc.titleString = programListModel.title;
                spvc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:spvc animated:YES];
            }
        }
    }
    else
    {
        YWAnchorHotListViewController *hlvc = [[YWAnchorHotListViewController alloc] init];
        hlvc.key = programListModel.key;
        hlvc.titleString = programListModel.title;
        hlvc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:hlvc animated:YES];
    }
}

- (void) getData;
{
    NSString *string = @"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=and-a1&device=android&includeActivity=true&includeSpecial=true&scale=2&statEvent=pageview%2Ftab%40发现_榜单&version=4.3.92";
    [LTNetTool GetNetWithURL:string Body:nil Header:nil Response:LTJSON Success:^(id result) {
        NSArray *datasArray = [result objectForKey:@"datas"];
        for (NSDictionary *dic in datasArray) {
            YWHotListModel *model = [[YWHotListModel alloc] initWithDic:dic];
            [_programListArray addObject:model];
        }
        [_hotListTableView reloadData];
        [_hotListTableView.mj_header endRefreshing];
    } Failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
